﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using YKXiangQi.Common;
using YKXiangQi.Enums;
using YKXiangQi.Models;
using Point = YKXiangQi.Models.Point;

namespace YKXiangQi.ViewModels
{
    [Serializable]
    public class PieceViewModel : ViewModelBase
    {
        public event EventHandler LeftMouseDown;
        public event EventHandler RightMouseDown;

        public PieceViewModel(Piece piece)
        {
            this.Piece = piece;
            LeftMouseClickCommand = new RelayCommand(OnLeftMouseDown);
            RightMouseClickCommand = new RelayCommand(OnRightMouseDown);
            State = PieceState.Normal;
        }

        public Piece Piece { get; private set; }
        public BoardSize BoardSize { get; set; }

        public ImageSource Image
        {
            get
            {
                string path = string.Format(@"/Images/{0}/{1}{2}.png",
                                            BoardSize.ToString(), Piece.Side.ToString()[0], Piece.Notation);
                return new BitmapImage(new Uri(path, UriKind.Relative));
            }
        }

        public ImageSource Mask
        {
            get
            {
                string path = string.Format(@"/Images/{0}/mask.png", BoardSize.ToString());
                return new BitmapImage(new Uri(path, UriKind.Relative));
            }
        }

        public int Size
        {
            get
            {
                switch (BoardSize)
                {
                    case BoardSize.Large:
                        return Constant.PieceLargeSize;
                    case BoardSize.Medium:
                        return Constant.PieceMediumSize;
                    case BoardSize.Small:
                    default:
                        return Constant.PieceSmallSize;
                }
            }
        }

        public int MaskSize
        {
            get
            {
                switch (BoardSize)
                {
                    case BoardSize.Large:
                        return Constant.PieceMaskLargeSize;
                    case BoardSize.Medium:
                        return Constant.PieceMaskMediumSize;
                    case BoardSize.Small:
                    default:
                        return Constant.PieceMaskSmallSize;
                }
            }
        }

        public int ModeSize
        {
            get
            {
                switch (BoardSize)
                {
                    case BoardSize.Large:
                        return Constant.PieceCollapsedModeLargeSize;
                    case BoardSize.Medium:
                        return Constant.PieceCollapsedModeMediumSize;
                    case BoardSize.Small:
                    default:
                        return Constant.PieceCollapsedSmallSize;
                }
            }
        }

        public ViewMode ViewMode
        {
            get { return Piece.ViewMode; }
            set
            {
                Piece.ViewMode = value;
                RaisePropertyChanged(() => ViewMode);
            }
        }

        private PieceState _state;
        public PieceState State
        {
            get { return _state; }
            set
            {
                _state = value;
                RaisePropertyChanged(() => this.State);
            }
        }

        public  SolidColorBrush Color
        {
            get
            {
                return Side == Sides.Red ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Black);
            }
        }

        private int _left;
        public int Left
        {
            get { return _left; }
            set
            {
                _left = value;
                RaisePropertyChanged(() => this.Left);
            }
        }

        private int _top;
        public int Top
        {
            get { return _top; }
            set
            {
                _top = value;
                RaisePropertyChanged(() => this.Top);
            }
        }

        public int X { get; set; }

        public int Y { get; set; }

        public Point Position
        {
            get { return new Point(X, Y); }
            set
            {
                X = value.X;
                Y = value.Y;
            }
        }

        public Sides Side
        {
            get { return Piece.Side; }
        }

        public RelayCommand LeftMouseClickCommand { get; private set; }
        protected void OnLeftMouseDown(object param)
        {
            if (LeftMouseDown != null)
                LeftMouseDown(this, null);
        }

        public RelayCommand RightMouseClickCommand { get; private set; }
        protected void OnRightMouseDown(object param)
        {
            if(RightMouseDown != null)
                RightMouseDown(this, new EventArgs());
        }

        public PieceViewModel Clone()
        {
            var result = new PieceViewModel(Piece.DeepClone())
                             {
                                 BoardSize = this.BoardSize,
                                 Position = this.Position,
                                 Left =  this.Left,
                                 Top =  this.Top,
                                 State = this.State
                             };

            return result;
        }

    }
}
